/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.unicauca.opentextbook.fachadas;

import edu.unicauca.opentextbook.entidades.Pertenece;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.LockTimeoutException;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;
import javax.persistence.PessimisticLockException;
import javax.persistence.Query;
import javax.persistence.QueryTimeoutException;
import javax.persistence.TransactionRequiredException;

/**
 *
 * @author Gamificaion
 */
@Stateless
public class PerteneceFacade extends AbstractFacade<Pertenece> {

    @PersistenceContext(unitName = "openTextBookPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public PerteneceFacade() {
        super(Pertenece.class);
    }

    public List<Pertenece> consultarPorGrupo(int idGrupo) {
        final StringBuilder sql = new StringBuilder();
        Query query;
        List<Pertenece> listaPertenece = null;

        sql.append("SELECT * ");
        sql.append("FROM pertenece p ");
        sql.append("WHERE p.idGrupo = ?");

        try {
            query = em.createNativeQuery(sql.toString(), Pertenece.class);
            query.setParameter(1, idGrupo);

            listaPertenece = query.getResultList();
        } catch (NoResultException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarPorGrupo_NoResultException:".concat(e.getMessage()));
            listaPertenece = new ArrayList<Pertenece>();
        } catch (IllegalStateException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarPorGrupo_IllegalStateException:".concat(e.getMessage()));
            listaPertenece = new ArrayList<Pertenece>();
        } catch (QueryTimeoutException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarPorGrupo_QueryTimeoutException:".concat(e.getMessage()));
            listaPertenece = new ArrayList<Pertenece>();
        } catch (TransactionRequiredException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarPorGrupo_TransactionRequiredException:".concat(e.getMessage()));
            listaPertenece = new ArrayList<Pertenece>();
        } catch (PessimisticLockException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarPorGrupo_PessimisticLockException:".concat(e.getMessage()));
            listaPertenece = new ArrayList<Pertenece>();
        } catch (LockTimeoutException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarPorGrupo_LockTimeoutException:".concat(e.getMessage()));
            listaPertenece = new ArrayList<Pertenece>();
        } catch (PersistenceException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarPorGrupo_PersistenceException:".concat(e.getMessage()));
            listaPertenece = new ArrayList<Pertenece>();
        } catch (Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarPorGrupo_Exception:".concat(e.getMessage()));
            listaPertenece = new ArrayList<Pertenece>();
        } finally {
            return listaPertenece;
        }
    }

    public Pertenece consultarSenior(int idGrupo) {
        final StringBuilder sql = new StringBuilder();
        Query query;
        Pertenece pertenece = null;

        sql.append("SELECT * ");
        sql.append("FROM pertenece p ");
        sql.append("WHERE p.idGrupo = ? ");
        sql.append("AND p.idRol = 1");

        try {
            query = em.createNativeQuery(sql.toString(), Pertenece.class);
            query.setParameter(1, idGrupo);

            pertenece = (Pertenece) query.getSingleResult();

        } catch (NoResultException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarSenior_NoResultException:".concat(e.getMessage()));
        } catch (NonUniqueResultException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarSenior_NonUniqueResultException:".concat(e.getMessage()));
        } catch (IllegalStateException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarSenior_IllegalStateException:".concat(e.getMessage()));
        } catch (QueryTimeoutException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarSenior_QueryTimeoutException:".concat(e.getMessage()));
        } catch (TransactionRequiredException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarSenior_TransactionRequiredException:".concat(e.getMessage()));
        } catch (PersistenceException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarSenior_PersistenceException:".concat(e.getMessage()));
        } catch (Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarSenior_Exception:".concat(e.getMessage()));
        } finally {
            return pertenece;
        }
    }
}
